package com.system.web.controller.main;

import com.system.web.common.entity.SimpleUser;
import com.system.web.controller.Urls;
import com.system.web.entity.JsonResp;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author <a href="mailto:liudajun0401@qq.com">DaJun.Liu</a>
 * @Description 用户管理控制器
 * @Data 2012-9-24
 * @Version 1.0.0  constructType
 */
@Controller
public class LoginController {

    @RequestMapping(value = Urls.LOGIN, method = RequestMethod.GET)
    public void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getRequestDispatcher("/views/login.html").forward(request,
                response);
    }

    @ResponseBody
    @RequestMapping(value = Urls.LOGIN, method = RequestMethod.POST)
    public JsonResp login(Model model, HttpSession httpSession, HttpServletRequest request, String username, String password) {
        try {
            SimpleUser user = new SimpleUser();
            user.setUsername("test");
            user.setPassword(DigestUtils.md5Hex("123456"));
            String md5Pwd = DigestUtils.md5Hex(password);
            if (user != null && user.getPassword().equals(md5Pwd)) {
                System.out.println("username--->" + username);
                System.out.println("password--->" + password);
                return JsonResp.success();
            }
        }catch (Exception e){
            return JsonResp.fail("账号密码不匹配!");
        }
        return JsonResp.fail("账号密码不匹配!");
    }
}
